#include "header.h"
#include "rt.h"

# define RAY		100

void				launch(GLuint program)
{
	SDL_Event		event;
	char			run = 1;
	int				time_last, time_act;
	int				nb_img;
	char			buffer[30];
	float			y, z, angle;

	nb_img = 0;
	angle = 0;
	time_act = time_last = SDL_GetTicks();
	while (run)
	{
		while (SDL_PollEvent(&event) && run)
		{
			if (event.type == SDL_QUIT || (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE))
				run = 0;
		}
		if (run)
		{
			y = 200 * cosf(angle);
			z = 200 * sinf(angle) + 250;
			shader_send_3f(program, "pos", 0.0, y, z); 
			angle += 0.02f;


			render();
			nb_img++;
			time_act = SDL_GetTicks();

			if (time_last + 1000 < time_act)
			{
				time_last = time_act;
				sprintf(buffer, "GLSL Raytracer %d FPS", nb_img);
				SDL_WM_SetCaption(buffer, NULL);
				nb_img = 0;
			}
			SDL_Delay(1);
		}
	}
}

int					main(int ac, char **av)
{
	SDL_Surface		*window;
	GLuint			program;

	if ((window = sdl_context()) == NULL)
		return (EXIT_FAILURE);
	gl_init();
	SDL_EnableKeyRepeat(10, 10);
	extend_init();
	if ((program = shader_create(NULL, "rt.frag")) == 0)
		return (EXIT_FAILURE);
	glUseProgram(program);
	launch(program);
	SDL_FreeSurface(window);
	SDL_Quit();
	return (EXIT_SUCCESS);
}